package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.api.feign.WorkMicroServiceFeign;
import com.imooc.enums.Sex;
import com.imooc.enums.ShowWhichName;
import com.imooc.enums.UserRole;
import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import com.imooc.service.UsersService;
import com.imooc.utils.DesensitizationUtil;
import com.imooc.utils.LocalDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 椋庨棿褰辨湀
 * @since 2025-09-10
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private WorkMicroServiceFeign workMicroServiceFeign;

	private static final String USER_FACE1 = "http://122.152.205.72:88/group1/M00/00/05/CpoxxF6ZUySASMbOAABBAXhjY0Y649.png";

	/**
	 * 根据手机号查询用户是否存在
	 * @param mobile 手机号码
	 * @return 返回匹配的用户对象，如果不存在则返回null
	 */
	@Override
	public Users queryMobileIsExist(String mobile) {
		// 使用MyBatis-Plus的QueryWrapper构建查询条件，根据手机号精确查询用户信息
		return usersMapper.selectOne(new QueryWrapper<Users>().eq("mobile", mobile));

	}

//	@GlobalTransactional
	@Transactional
	@Override
	public Users createUsers(String mobile) {
		Users user = new Users();

		user.setMobile(mobile);
		user.setNickname("用户" + DesensitizationUtil.commonDisplay(mobile));
		user.setRealName("用户" + DesensitizationUtil.commonDisplay(mobile));
		user.setShowWhichName(ShowWhichName.nickname.type);

		user.setSex(Sex.secret.type);
		user.setFace(USER_FACE1);
		user.setEmail("");

		LocalDate birthday = LocalDateUtils
				.parseLocalDate("1980-01-01",
						LocalDateUtils.DATE_PATTERN);
		user.setBirthday(birthday);

		user.setCountry("中国");
		user.setProvince("");
		user.setCity("");
		user.setDistrict("");
		user.setDescription("这家伙很懒，什么都没留下~");

		// 我参加工作的日期，默认使用注册当天的日期
		user.setStartWorkDate(LocalDate.now());
		user.setPosition("底层码农");
		user.setRole(UserRole.CANDIDATE.type);
		user.setHrInWhichCompanyId("");
		user.setCreatedTime(LocalDateTime.now());
		user.setUpdatedTime(LocalDateTime.now());
		usersMapper.insert(user);

		// 发起远程调用 完成初始化简历
		// todo 存在分布式使用失效问题。
		workMicroServiceFeign.init(user.getId());
//		int a = 10 / 0;
		return user;
	}

}
